<template>
    <div class="sidebar">

        <el-menu
            class="sidebar-el-menu"
            :default-active="onRoutes"
            :collapse="collapse"
            background-color="#324157"
            text-color="#bfcbd9"
            active-text-color="#20a0ff"
            unique-opened
            router
        >
            <template v-for="item in items">
                <!-- 是否存在二级菜单 -->
                <template v-if="item.menuList">
                    <!-- 展示一级菜单 -->
                    <el-submenu :index="'/'.concat(item.id)" :key="item.id">
                        <template #title>
                            <i :class="item.icon"></i>
                            <span>{{ item.name }}</span>
                        </template>
                        <!-- 一级菜单中的二级菜单 -->
                        <template v-for="subItem in item.menuList">
                            <!-- 二级菜单中是否包含三级菜单 -->
                            <el-submenu
                                v-if="subItem.menuList"
                                :index="subItem.url"
                                :key="subItem.id"
                            >
                                <template #title>{{ subItem.name }}</template>
                                <!--<el-menu-item
                                    v-for="(threeItem, i) in subItem.menuList"
                                    :key="i"
                                    :index="threeItem.url"
                                >{{ threeItem.name }}</el-menu-item>-->
                            </el-submenu>
                            <el-menu-item
                                v-else
                                :index="subItem.url"
                                :key="subItem.id"
                            >
                                {{ subItem.name }}
                            </el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="'/'.concat(item.id)" :key="item.id">
                        <i :class="item.icon"></i>
                        <template #title>{{ item.name }}</template>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
export default {
    data() {
        return {
            items:[]
        };
    },
    computed: {
        onRoutes() {
            return this.$route.path.replace("/", "");
        },
        collapse(){
            console.log("collapse*********",this.$store)
            return this.$store.state.collapse
        }
    },
    mounted() {
        //获取loginUser
       const loginUser = JSON.parse(localStorage.getItem('loginUser'));
       //根据角色选择使用哪个菜单
       // this.items = loginUser.role=="manager"?this.managerItems:this.businessItems
        this.$axios({
            method: 'get',
            url: '/menu/roleMenu/'+loginUser.roleId,
        }).then(res=>{
            if (res.data.code == 200){
                this.items = res.data.data;
            }
        }).catch(error=>{

        })
    },
};
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
}
.sidebar > ul {
    height: 100%;
}
</style>
